import 'dart:convert';

import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ncsjqz_getx/http/dio_service.dart';
import 'package:flutter_ncsjqz_getx/model/base_entity.dart';
import 'package:flutter_ncsjqz_getx/model/login_entity.dart';
import 'package:flutter_ncsjqz_getx/model/user_detail_entity.dart';
import 'package:flutter_ncsjqz_getx/page/home/home_view.dart';
import 'package:flutter_ncsjqz_getx/page/my/email_phone_pwd/email_password_view.dart';
import 'package:flutter_ncsjqz_getx/page_common/show_html_page.dart';
import 'package:flutter_ncsjqz_getx/util/get_user_util.dart';
import 'package:flutter_ncsjqz_getx/util/toast_util.dart';
import 'package:get/get.dart';
import 'package:package_get/base_logic.dart';

class LoginLogic extends BaseLogic {
  // 1-手机号密码,2-手机号验证码)
  int loginType = 2;

  // true 登录  false 注册
  bool loginRegister = true;

  // true 显示密码 false 不显示
  bool showPwd = true;

  // 性别 0-未知 1-男 2-女
  int sex = 2;

  //REG_SMS_CODE(1, "注册"),
  //LOGIN_SMS_CODE(2, "登录"),
  //PWD_SMS_CODE(3, "找回密码");
  int codeType = 1;

  // 是否勾选协议
  bool checkProtocol = false;

  TextEditingController textEditingControllerTel = TextEditingController();
  TextEditingController textEditingControllerPwd = TextEditingController();
  TextEditingController textEditingControllerPwd1 = TextEditingController();
  TextEditingController textEditingControllerCode = TextEditingController();
  TextEditingController textEditingControllerAddress = TextEditingController();
  TextEditingController textEditingControllerBirthday = TextEditingController();


  restTextEditingController() {
    textEditingControllerTel.text = "";
    textEditingControllerPwd.text = "";
    textEditingControllerPwd1.text = "";
    textEditingControllerCode.text = "";
    textEditingControllerAddress.text = "";
    textEditingControllerBirthday.text = "";
  }

  setSex(value) {
    sex = value;
    update();
  }

  setLoginType() {
    loginType = loginType == 1 ? 2 : 1;
    restTextEditingController();
    loginRegister = true;
    update();
  }

  setShowPwd() {
    showPwd = !showPwd;
    update();
  }

  setLoginRegister() {
    loginRegister = !loginRegister;
    restTextEditingController();
    update();
  }

  /// todo：发送验证码
  sendAuthCode() async {
    if (textEditingControllerTel.text == "") return toast("手机号未输入");
    Map<String, dynamic> map = Map();
    map['telPhone'] = textEditingControllerTel.text;
    map['smsCodeType'] = codeType;
    BaseEntity beanEntity = await DioService<BaseEntity>().getSmsCode(map,);
    if (beanEntity.code == 0) {
      toast("发送验证码成功");
      startTimeDown();
    } else {
      toast("发送验证码失败");
    }
  }

  /// todo：注册事件
  onRegister() async {
    if (textEditingControllerPwd.text == "") return toast("密码不能为空");
    if (textEditingControllerPwd1.text == "") return toast("确认密码不能为空");
    if (textEditingControllerPwd1.text != textEditingControllerPwd.text) return toast("两次输入密码不一致");
    if (textEditingControllerAddress.text == "") return toast("居住地址不能为空");
    if (textEditingControllerBirthday.text == "") return toast("出生年月日不能为空");
    if (!checkProtocol) return toast("请阅读并同意勾选《用户协议》和《隐私政策》");
    Map<String, dynamic> map = Map();
    map['birthDay'] = '';
    map['birthMonth'] = '';
    map['birthYear'] = '';
    map['birth'] = textEditingControllerBirthday.text;
    map['sex'] = sex;
    map['smsCode'] = textEditingControllerCode.text;
    map['telPhone'] = textEditingControllerTel.text;
    map['passWord'] = textEditingControllerPwd.text;
    map['nickName'] = '暂未设置昵称';
    map['userMail'] = 'example@example.com';
    map['userPic'] = "http://cdn.lnong.com.cn/FnekOzonVyz6aiNWuYHBS92ogxo5";
    BaseEntity data = await DioService<BaseEntity>().checkAndRegister(map);
    if (data.code == 0) {
      toast("注册成功");
      loginType = 1;
      loginRegister = true;
      restTextEditingController();
      update();
    }else{
      toast("注册失败：${data.msg}");
    }
  }

  /// todo：登录事件
  /// [isWx]微信登录 ,[isAli] 支付宝登录 ，默认登录类型：loginType
  onLogin({bool isWx:false,isAli:false,code}) async {
    if (textEditingControllerPwd.text == "" && loginType==1 && !isWx) return toast("密码不能为空");
    if (!checkProtocol) return toast("请阅读并同意勾选《用户协议》和《隐私政策》");
    Map<String, dynamic>  map = {
      "accountType": 0,
      "loginType": isWx ? 3 : loginType,
    };

    if(isWx){
      // 微信登录code
      map["code"] =code;
    }else{
      map["telPhone"] = textEditingControllerTel.text;
      if(loginType == 1){
        map["passWord"] = textEditingControllerPwd.text;
      }
      if(loginType == 2){
        map["smsCode"] = textEditingControllerCode.text;
      }
    }

    LoginEntity data = await DioService<LoginEntity>().login(map);
    if (data.code == 0) {
      toast("登录成功");
      /// 登录信息持久化
      await setUserUtil(data.data ?? '');
      Get.off(()=>HomePage());
    }else{
      toast("${data.msg}");
    }
  }

  /// 获取协议
  getProtocol(title) async{
    BaseEntity beanEntity = await DioService<BaseEntity>().protocol({
      "protocolCode": title == '用户协议' ?"user":"policy"
    });
    if(beanEntity.code == 0){
      Get.to(()=>ShowHtmlPage(title: "${beanEntity.data["title"]}",htmlStr: "${beanEntity.data["content"]}",),preventDuplicates: false);
    }
  }
}